package com.wdl.modules.myhouse.utils.util;

import org.apache.tomcat.util.http.fileupload.IOUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * 视频工具类
 */
public class VideoUtil {

    public static Integer connectionTimeOut=1000;
    public static Integer readTimeOut=5000;

    /**
     * 将url的视频转换到response的输出流
     * @param url1 视频url
     * @param response
     * @return 带有输入数据的response
     */
    public static HttpServletResponse copyStreamToResponse(String url1, HttpServletResponse response) {
        //创建连接对象
        try {
            URL url = new URL(url1);
            URLConnection conn = url.openConnection();
            //设置超时
            conn.setConnectTimeout(connectionTimeOut);
            conn.setReadTimeout(readTimeOut);
            //发起连接
            conn.connect();
            //获取流
            InputStream inputStream = conn.getInputStream();
            //流转换
            IOUtils.copy(inputStream, response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        //设置返回类型
        return response;
    }

}
